<template>
  <div class="stat-container">
    <!-- 设备table -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <!-- <el-form-item label="站点名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入站点名称" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-download" size="mini" @click="exportData"
          >导出</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableList">
      <el-table-column label="订单号" prop="orderNo" />
      <el-table-column label="订单金额(元)" prop="orderAmount">
        <template slot-scope="scope">
          <span>{{ scope.row.orderAmount / 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款金额(元)" prop="refundAmount">
        <template slot-scope="scope">
          <span>{{ scope.row.refundAmount / 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手续费总金额(元)" prop="serviceCharge">
        <template slot-scope="scope">
          <span>{{ scope.row.serviceCharge / 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" prop="payTime" />
      <el-table-column label="创建时间" prop="orderCreateTime" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10, 20, 30, 50, 500, 1000]"
    />
  </div>
</template>

<script>
import { getServiceAnalysisDetailAPI } from "@/api/charge/serviceAnalysis";
import { exportExcel, exportExcelFromAPI } from "@/utils/utils";

export default {
  name: "Order",
  props: {
    dateArr: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      editVisible: false,
      // 是否显示弹出层（数据权限）
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],

      payTypeMap: {
        WECHAT: "微信支付",
        ALIPAY: "支付宝支付",
        WALLET: "钱包",
        PHYSICAL_CARD: "实体卡支付",
      },
      chargingStatusMap: {
        FINISH: "已完成",
        CHARGING: "充电中",
        NO_PLUGGED: "未插电",
      },

      // 站点搜索关键词
      keywords: "",
      curSiteId: "",

      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        siteId: [{ required: true, message: "请输入站点id", trigger: "blur" }],
      },

      // 设备收费模式
      chargeForm: {},

      // 全部站点列表
      siteList: [],

      stationData: {},
    };
  },
  created() {
    this.getList();
  },
  watch: {
    curSiteId() {
      this.getList();
    },
  },
  methods: {
    exportData() {
      exportExcelFromAPI("/statistics/data/getOrderStatisticsPage/export", {
        ...this.queryParams,
        pageNum: 1,
        pageSize: 100000,
      });
      return false;
      exportExcel({
        data: this.formatTableList(this.tableList),
        headers: [
          { label: "订单号", key: "orderNo" },
          { label: "订单金额(元)", key: "orderAmount" },
          { label: "退款金额(元)", key: "refundAmount" },
          { label: "服务费总金额(元)", key: "serviceCharge" },
          { label: "支付时间", key: "payTime" },
          { label: "创建时间", key: "orderCreateTime" },
        ],
        filename: "分账数据详情.xlsx",
      });
    },
    // 格式化表格数据
    formatTableList(data) {
      return data.map((item) => {
        return {
          ...item,
          orderAmount: item.orderAmount / 100,
          refundAmount: item.refundAmount / 100,
          serviceCharge: item.serviceCharge / 100,
        };
      });
    },
    // 编辑成功回调
    onEditSuccess() {
      this.editVisible = false;
      this.getList();
    },
    // 编辑关闭回调
    onEditCancel() {
      this.editVisible = false;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getServiceAnalysisDetailAPI({
        ...this.queryParams,
        startTime: this.dateArr[0],
        endTime: this.dateArr[1],
      }).then((res) => {
        this.tableList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("确认删除吗？")
        .then(function () {
          return deleteAPI([row.id]);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 跳转详情页 */
    toDetail(row) {
      this.$router.push({
        path: "station/edit",
        query: {
          stationName: row.stationName,
        },
      });
    },
  },
};
</script>
<style scoped lang="scss">
.stat-container {
  height: 100%;
  box-sizing: border-box;

  .site-list-container {
    width: 20%;
    flex: none;
    margin-right: 20px;

    .site-list-wrapper {
      position: relative;
      overflow: hidden;

      .site-search {
        position: absolute;
        top: 0;
        margin-bottom: 20px;
      }

      .site-list {
        margin-top: 40px;
        overflow: auto;

        .site-btn {
          width: 100%;
          font-size: 12px;
          color: #606266;
          padding: 9px 15px;
          text-align: left;
          border-radius: 2px;
          cursor: pointer;

          &:hover {
            color: #1890ff;
            background-color: #e8f4ff;
          }

          &.selected {
            color: #1890ff;
            background-color: #e8f4ff;
          }
        }
      }
    }
  }

  .station-list-container {
    width: 80%;
    flex: auto;
  }
}
</style>
